﻿// <copyright file="BlogEntry.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>


//All propertiex should be virtual according http://msdn.microsoft.com/en-us/library/dd468057.aspx

namespace CleverCMS.Blog.Data.Entities
{
    using System;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public class BlogEntry 
    {  
        public BlogEntry() 
        {
            Published = DateTime.Now;
            Content = String.Empty;
        }
        
        [Key]
        public virtual int Id { get; set; }
        /// <summary>
        /// Gets or sets the creation date.
        /// </summary>
        [Required]
        public virtual DateTime Created { get; set; }
        /// <summary>
        /// Gets or sets the modification date.
        /// </summary>
        public virtual DateTime? Modified { get; set; }
        /// <summary>
        /// Feed Id
        /// </summary>
        public virtual int? FeedId { get; set; }
        /// <summary>
        /// Category Id
        /// </summary>
        public virtual int? CategoryId { get; set; }
        /// <summary>
        /// The header.
        /// </summary>
        private string header;
        /// <summary>
        /// Gets or sets the header.
        /// </summary>
        [Required(ErrorMessage = "Missing a Header. Check title.")]
        public virtual string Header
        {
            get
            {
                return this.header;
            }
            set
            {
                if (value != null)
                {
                    this.header = value.Trim();
                }
            }
        }
        /// <summary>
        /// The headerUrl.
        /// </summary>
        private string headerUrl;
        /// <summary>
        /// Gets or sets the header URL.
        /// </summary>
        [Required(ErrorMessage = "Missing a HeaderUrl. Check title.")]
        public virtual string HeaderUrl
        {
            get
            {
                return this.headerUrl;
            }
            set
            {
                if (value != null)
                {
                    this.headerUrl = value;
                    this.Alias = this.headerUrl;
                }
            }
        }
        /// <summary>
        /// Gets or sets the Marker.
        /// </summary>
        public virtual string Marker { get; set; }
        /// <summary>
        /// Gets or sets the author.
        /// </summary>
        public virtual string Author { get; set; }
        /// <summary>
        /// Gets or sets the short content.
        /// </summary>
        [AllowHtml]
        public virtual string ShortContent { get; set; }
        /// <summary>
        /// Gets or sets the short content.
        /// </summary>
        [AllowHtml]
        public virtual string ShortContentOriginal { get; set; }
        /// <summary>
        /// Gets or sets the content.
        /// </summary>
        [AllowHtml]
        public virtual string Content { get; set; }
        /// <summary>
        /// Gets or sets the content.
        /// </summary>
        [AllowHtml]
        public virtual string ContentOriginal { get; set; }
        /// <summary>
        /// Seo Title
        /// </summary>
        public virtual string SeoTitle { get; set; }
        /// <summary>
        /// Meta description
        /// </summary>
        public virtual string MetaDescription { get; set; }
        /// <summary>
        /// Meta Keywords
        /// </summary>
        public virtual string MetaKeywords { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="BlogEntry"/> is visible.
        /// </summary>
        public virtual bool Visible { get; set; }
        /// <summary>
        /// Indicates Blogentry has comments block or not
        /// </summary>
        public virtual bool ShowRating { get; set; } 
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="BlogEntry"/> is visible.
        /// </summary>
        public virtual bool? Indexed { get; set; }
        /// <summary>
        /// Gets or sets the alias for entry.
        /// </summary>
        public virtual string Alias { get; set; }
        /// <summary>
        /// Entry published date
        /// </summary>
        public virtual DateTime? Published { get; set; }
        /// <summary>
        /// Gets or sets the blog entry comments.
        /// </summary>
        public virtual int ViewCount { get; set; }  
        /// <summary>
        /// Connected Feed 
        /// </summary>
        [ForeignKey("FeedId")]
        public virtual Feed Feed { get; set; }
        /// <summary>
        /// Connected Category
        /// </summary>
        [ForeignKey("CategoryId")]
        public virtual BlogEntryCategory Category { get; set; }
        /// <summary>
        /// Gets or sets the tags.
        /// </summary>
        public virtual ICollection<BlogEntryTag> BlogEntryTags { get; set; }
        /// <summary>
        /// Gets or sets the blog entry comments.
        /// </summary>
        public virtual ICollection<BlogEntryComment> BlogEntryComments { get; set; }


        #region Extensions
        /// <summary>
        /// Gets the URL / permalink of the <see cref="BlogEntry"/>.
        /// </summary>
        [NotMapped]
        public string Url
        {
            get
            {
                return "/blog/" + SiteUrl();
            }
        }
       
        public string SiteUrl()
        {
            return (this.Category == null ? "main" : this.Category.CategoryName.ToLower()) + "/entry/" + this.HeaderUrl;
        }

        [NotMapped]
        public ValueProviderResult TagString { get; set; }
        #endregion
    }
}